package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
     return  R.success("下单成功");
    }

    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime,String endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        log.info("=============>",pageInfo);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.like(number!=null,Orders::getNumber,number);
        lqw.between(beginTime!=null,Orders::getOrderTime,beginTime,endTime);


        List<Orders> orders = orderService.list(lqw);
        pageInfo.setRecords(orders);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<Orders> put(@RequestBody Orders orders){
       // log.info("============>{}",orders);
        orderService.updateById(orders);
        return R.success(orders);
    }

    @GetMapping("userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize, HttpSession session){
        Object userId = session.getAttribute("user");
        Page<Orders> pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,userId);
        lqw.orderByDesc(Orders::getOrderTime);
        List<Orders> orders = orderService.list(lqw);

        Page<OrdersDto> result=new Page<>();
        BeanUtils.copyProperties(pageInfo,result,"records");
        List<OrdersDto> resutl=new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto ordersDto=new OrdersDto();
            BeanUtils.copyProperties(order,ordersDto);
            Long id = order.getId();
            LambdaQueryWrapper<OrderDetail> ddlqw = new LambdaQueryWrapper<>();
            ddlqw.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> list = orderDetailService.list(ddlqw);
            ordersDto.setOrderDetails(list);
            int sumNum=0;
            for (OrderDetail orderDetail : list) {
                sumNum+=orderDetail.getNumber();
            }
            ordersDto.setSumNum(sumNum);
            resutl.add(ordersDto);
        }
        result.setRecords(resutl);
        log.info("==================>"+result);
        return R.success(result);
    }
}